package com.webserver.http;

import java.io.*;
import java.net.Socket;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 响应对象
 * 该类的每一个实例用于表示服务端发送的一个HTTP响应内容。每个响应由三部分构成
 * 状态行
 */
public class HttpResponse {

    //状态行相关信息
   private int statusCode=200;
    private String statusReason="OK";
    //响应头相关信息
    private Map<String,String> headers=new HashMap<>();
    //响应正文相关信息
    private Socket socket;
    public HttpResponse(Socket socket){
        this.socket=socket;
    }
    private File entity;//响应正文相关信息
    public void flush() throws IOException {
        //发送状态行
        sendStatusLine();
        //发送响应头
        sendHeaders();
        //发送响应正文
        sendContent();
    }
    private void sendStatusLine() throws IOException {
        String line=" HTTP/1.1"+" "+statusCode+" "+statusReason;
       printlin(line);
    }
    private void sendHeaders() throws IOException {
        Set<Map.Entry<String,String>> entrySet=headers.entrySet();
        for (Map.Entry<String,String> e:entrySet){
            String name=e.getKey();
            String value=e.getValue();
            String line=name+": "+value;
            printlin(line);
        }
        printlin("");//空字符串
    }
    private void sendContent() throws IOException {
        try(
        FileInputStream fi=new FileInputStream(entity);
        )
        {
        OutputStream out=socket.getOutputStream();
        byte[] data=new byte[1024*10];
        int len;
        while ((len=fi.read(data))!=-1) {
            out.write(data,0,len);
        }
        System.out.println("响应完毕");
        }//这里不写catch是因为异常抛出给流程控制来处理
    }
    private void printlin(String Line) throws IOException {
        OutputStream out=socket.getOutputStream();
            /*响应的大致内容:
            HTTP/1.1 200 OK(CRLF)
            Content-Type: text/html(CRLF)
            Content-Length: 2546(CRLF)(CRLF)
                    1011101010101010101......
*/
        byte[] data=Line.getBytes("ISO8859-1");
        out.write(data);
        out.write(13);//回车
        out.write(10);//换行
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getEntity() {
        return entity;
    }

    public void setEntity(File entity) {//添加响应正文的实体文件
        //添加该文件的同时会根据实体文件自动添加两个说明正文的响应头
        this.entity = entity;
        //根据entity获取该文件的文件名
        //根据文件名获取后缀名
        //根据后缀名获取Content-Type的
        String name=entity.getName();
        String fileName=name.substring(name.lastIndexOf(".")+1);
      /* Map<String,String> mimeMapping=new HashMap<>();
        mimeMapping.put("html","text/html");
        mimeMapping.put("css","text/css");
        mimeMapping.put("js","application/javascript");
        mimeMapping.put("png","image/png");
        mimeMapping.put("gif","image/gif");
        mimeMapping.put("jpg","image/jpeg");*/

        //String Type=mimeMapping.get(fileName);
        String Type=HttpContext.getMimeType(fileName);
        System.out.println(fileName+"的类型为"+Type);
        putHeader("Content-Type",Type);
        putHeader("Content-Length",entity.length()+"");
    }
    public void putHeader(String name,String value ){
        this.headers.put(name,value);
    }
}
